import re from django.template import Library
 
 register = Library()
 
 def parse_args_kwargs_and_as_var(parser, bits):
 
     args = []
     kwargs = {}
     as_var = None
     
     bits = iter(bits)
     for bit in bits:
         if bit == 'as':
             as_var = bits.next()
             break            
         else:
             m = re.search('''(\w+)=("[^<>"]*"|'[^<>']*'|\w+)''', bit)
             if (m):
                 k, v = bit.split('=', 1)
                 k = k.strip()
                 kwargs[k] = parser.compile_filter(v)
             else:
                 for arg in bit.split(","):
                     if '=' in arg:
                         k, v = arg.split('=', 1)
                         k = k.strip()
                         kwargs[k] = parser.compile_filter(v)
                     elif arg:
                         args.append(parser.compile_filter(arg))
     return args, kwargs, as_var
 
